package com.e_manager.soft.emanager.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * [dataTransformationUtil]<br>
 * 报文数据转换<br>
 * <br>
 * [功能概要]<br>
 * 报文数据转换<br>
 * <br>
 * [更新履历]<br>
 * 2018/03/08 ver1.00 JYS CORPORATION<br>
 *
 * @author JYS CORPORATION
 * @version 1.00
 */
public class DataTransformationUtil {
    /**
     *
     * 报文数据转换
     *
     * @Title: dataToMap
     * @Description:
     * @return Map<String,Object>
     * @throws
     */
    public static Map<String, Object> dataToMap(String hexString) {
        //截取标识位
        String flag = hexString.substring(4,6);
        //初始化对象
        Map<String,Object> map = new HashMap<String,Object>();
        List strList = new ArrayList();
        Float  temperature = 0f;
        Float  humidity = 0f;
        Float  voltage  = 0f;
        String  day = "" ;
        int start = 0;
        int end = 0;

        String warnType = ""; //报警类型
        //正常数据,报文类型messageType=0
        if(flag.equals("00")){
            //去掉头与标示位开始自动截取4位
            start = 6;
            end = start+4;
            map.put("messageType","0");
            map.put("warnType","");
        }
        //报警数据，报文类型messageType=1
        if(flag.equals("08") || flag.equals("1D")){
            String data = flag;
            //转化为2进制
            String warnData = String.valueOf(Integer.parseInt(hexString2binaryString(data)));
            //循环取出报警类型
            for (int i = 0; i < warnData.length(); i++) {
                char item = warnData.charAt(i);
                if(String.valueOf(item).equals("1")){
                    warnType = warnType + "," + String.valueOf(warnData.length()-i-1);
                }
            }
            map.put("messageType","1");
            map.put("warnType",warnType.substring(1,warnType.length()));
            //去掉头与标示位与报警数据开始自动截取4位
            start = 6;
            end = start+4;
        }

        //自动截取4位放到集合里
        while(true){
            if(start>=hexString.length())
                break;
            String temp = hexString.substring(start, end);
            strList.add(temp);
            start = end;
            end = end+4;
            if(end>=hexString.length()){
                end = hexString.length();
            }
        }
        //取温度数据转为10进制然后转化为正常数据
        temperature = Float.parseFloat(hexString10binaryString(String.valueOf(strList.get(0))))/100;
        //取湿度数据转为10进制然后转化为正常数据
        humidity = Float.parseFloat(hexString10binaryString(String.valueOf(strList.get(1))))/100;
        //取电压数据转为10进制然后转化为正常数据
        voltage = Float.parseFloat(hexString10binaryString(String.valueOf(strList.get(2))))/100;
        //取运行天数数据转为10进制然后转化为正常数据
        day = hexString10binaryString(String.valueOf(strList.get(strList.size()-1)));
        //放到map里返回
        map.put("temperature",temperature);
        map.put("humidity",humidity);
        map.put("voltage",voltage);
        map.put("day",day);

        return map;
    }
    //北京硬件报警
    public static Map<String, Object> dataStrToMap(String hexString){
        //初始化对象
        Map<String,Object> map = new HashMap<String,Object>();
        String flag = hexString.substring(2,hexString.length());
        Float voltage = Float.parseFloat(hexString10binaryString(flag))/100;
        map.put("messageType","1");
        map.put("warnType",2);
        map.put("voltage",voltage);
        return map;
    }
    /**
     *
     * 将16进制转为2进制
     *
     * @param hexString
     * @return
     */
    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(hexString
                    .substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }

    /**
     *
     * 将16进制转10进制
     *
     * @param hexString
     * @return
     */
    public static String hexString10binaryString(String hexString) {
        String bString = String.valueOf(Integer.parseInt(hexString, 16));
        return bString;
    }
}
